//
//  UITableViewRereshProtocol.swift
//  FlameB
//
//  Created by 连俊 on 2020/8/4.
//  Copyright © 2020 连俊. All rights reserved.
//

protocol UITableViewRereshProtocol: class {
    var tableView: UITableView{get}
    var curPageIndex: Int{get set}
    func startLoadPage(with index:Int)
    func refreshUI(with index: Int, isHasNextPage: Bool, hiddenFooter: Bool)
    func setHeadAndFooterRefresh()
    func enableHeadRefresh(_ tableView: UITableView, enable: Bool)
    func enableFootRefresh(_ tableView: UITableView, enable: Bool)
    func loadFirstPage()
    func reset()
    func setPlaceholdView(isNoContent:Bool, promot: String , imageName: String)
}

extension UITableViewRereshProtocol {
    /// 请求数据拿到之后设置 上拉 下拉的 状态
    ///
    /// - Parameters:
    ///   - index: 当前请求的页码序号
    ///   - isHasNextPage: 请求的页的条目个数 == pagesize -> true 说明还有下一页
    ///   - hiddenFooter: 没有更多页时,是否隐藏 footer
    func refreshUI(with index: Int, isHasNextPage: Bool, hiddenFooter: Bool) {
        self.curPageIndex = index
        if index == 1 {
            self.stopHeaderRefresh()
            self.reset()
            if isHasNextPage{
                self.tableView.mj_footer?.isHidden = false
            } else {
                self.tableView.mj_footer?.isHidden = true
            }
        } else {
            if isHasNextPage {
                self.stopFootRefresh(isLastPage: false)
            } else {
                self.stopFootRefresh(isLastPage: true, hiddenFooter: hiddenFooter)
            }
        }
    }
    
    func setHeadAndFooterRefresh() {
        self.enableHeadRefresh(tableView, enable: true)
        self.enableFootRefresh(tableView, enable: true)
        tableView.mj_footer?.isHidden = true //初始隐藏
    }
    
    //tableView  header
    func enableHeadRefresh(_ tableView: UITableView, enable: Bool) {
        guard let _ = tableView.mj_header else {
            if !enable {
                return
            }
            let header = GifHeader(refreshingBlock: {[weak self] in
                tableView.mj_footer?.isHidden = !tableView.isPagingEnabled
                self?.loadFirstPage()
            })
//            guard let header = GifHeader(refreshingBlock: { [weak self] in
//                tableView.mj_footer.isHidden = !tableView.isPagingEnabled
//                self?.loadFirstPage()
//            }) else {
//                return
//            }
            
            header.setTitle("下拉刷新", for: .idle)
            header.setTitle("更新中", for: .refreshing)
            //header.stateLabel.isHidden = true;
            header.lastUpdatedTimeLabel?.isHidden = true
            header.isAutomaticallyChangeAlpha = true
            tableView.mj_header = header
            return
        }
        tableView.mj_header?.isHidden = !enable
    }
    
    
    func enableFootRefresh(_ tableView: UITableView, enable: Bool) {
        guard let _ = tableView.mj_footer else {
            if !enable {
                return
            }
            let footer = MJRefreshAutoNormalFooter(refreshingBlock: { [weak self] in
                self?.loadMoreData()
            })
            footer.setTitle("上拉加载更多", for: .idle)
            footer.setTitle("加载中...", for: .refreshing)
            footer.setTitle("没有更多数据...", for: .noMoreData)
            tableView.mj_footer = footer
            return
        }
        tableView.mj_footer?.isHidden = !enable
    }
    
    
    func loadFirstPage(){
        curPageIndex = 1
        startLoadPage(with: curPageIndex)
    }
    
    func loadMoreData() {
        startLoadPage(with: curPageIndex+1)
    }
    
    func stopFootRefresh(isLastPage:Bool, hiddenFooter: Bool = false) {
        if isLastPage {
            tableView.mj_footer?.endRefreshingWithNoMoreData()
            tableView.mj_footer?.isHidden = hiddenFooter
        } else {
            tableView.mj_footer?.endRefreshing()
        }
    }
    
    func stopHeaderRefresh() {
        self.tableView.mj_header?.endRefreshing()
    }
    
    func reset() {
        self.tableView.mj_footer?.resetNoMoreData()
    }
    
    func setPlaceholdView(isNoContent:Bool, promot: String , imageName: String ) {
        if isNoContent {
            let noConentView = UIView(frame: self.tableView.bounds)
            self.tableView.backgroundView = noConentView
        } else {
            self.tableView.backgroundView = nil
        }
    }
}
